package services;

import dao.Ship_Repository;
import models.BerthingSchedule_T_Entity;
import models.DatabaseExecutionContext;
import models.ShipInfo_T_Entity;
import play.db.jpa.JPAApi;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletionStage;

import static java.util.concurrent.CompletableFuture.supplyAsync;

public class Ship_Repository_JPA implements Ship_Repository {
    private final JPAApi jpaApi;
    private final DatabaseExecutionContext executionContext;

    @Inject
    public Ship_Repository_JPA(JPAApi jpaApi, DatabaseExecutionContext executionContext) {
        this.jpaApi = jpaApi;
        this.executionContext = executionContext;
    }

    @Override
    public CompletionStage<List<ShipInfo_T_Entity>> getShipInfo ()
    {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
//                String strSql = "exec p_CreateDateReport @PhoneNumber = '"+PhoneNumber+"',@StartDate = '" + StartDate + "',@EndDate = '"+ EndDate+"',@HWMC = '"+HWMC+"',@KHMC ='"+KHMC+"',@MonthFlag ='"+MonthFlag+"' ";
                String strSql = "SELECT * FROM t_ShipInfo";
                return entityManager.createNativeQuery(strSql, ShipInfo_T_Entity.class).getResultList();


            });
        }, executionContext);
    }

    @Override
    public CompletionStage<List<BerthingSchedule_T_Entity>> getBerthingSchedule ()
    {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                String strSql = "SELECT * FROM t_BerthingSchedule order by fd_BerthOrder";
                return entityManager.createNativeQuery(strSql, BerthingSchedule_T_Entity.class).getResultList();


            });
        }, executionContext);
    }

    public CompletionStage<List<Object>> updateBerthSKD (String BerthOrders, String ApplyIDs, String Operator, String OperateFlag)
    {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                String strSql=null;
                List<Object> resultList = new ArrayList<Object>();


                    strSql = "exec dbo.p_UpdateBerthSKD @BerthOrders = '"+BerthOrders+"',@ApplyIDs ='"+ApplyIDs+"',@Operator = '"+Operator+"',@OperateFlag = '"+OperateFlag+"'";
                    List<BerthingSchedule_T_Entity>  list =  entityManager.createNativeQuery(strSql, BerthingSchedule_T_Entity.class).getResultList();
                    resultList.add(list);


                return resultList;
            });
        }, executionContext);
    }


}
